

    $(document).ready(function(){
        initPagination();
    });


    //条件查询开始
    $("#distpicker1").distpicker({
        autoSelect: false
    });


    var datas = [
        {
            name:'客户姓名',
            className:'.client-name',
            parameter:''
        },
        {
            name:'手机号码',
            className:'.phone-num',
            parameter:''
        },
        {
            name:'当前专属律师',
            className:'.current-lawyer',
            parameter:''
        },
        {
            name:'咨询总次数start',
            className:'.start-num',
            parameter:''
        },
        {
            name:'咨询总次数end',
            className:'.end-num',
            parameter:''
        },
        {
            name:'创建时间开始',
            className:'#create-start',
            parameter:''
        },
        {
            name:'创建时间结束',
            className:'#create-end',
            parameter:''
        },
        {
            name:'首次咨询时间开始',
            className:'#first-consult-start',
            parameter:''
        },
        {
            name:'首次咨询时间结束',
            className:'#first-consult-end',
            parameter:''
        },
        {
            name:'末次咨询时间开始',
            className:'#last-consult-start',
            parameter:''
        },
        {
            name:'首次咨询时间结束',
            className:'#last-consult-end',
            parameter:''
        }
    ]
    //获取搜索条件
    $('.seek-btn').on('click',function(){
        initPagination();
    });
    //清空条件
    $('.seek-clear').on('click',function(){
        for(var i = 0 ; i<datas.length;i++){
            var data = datas[i];
            $(data.className).val('');
        }
        $('.province option:eq(0)').prop('selected',true)
        $('.city option:eq(0)').prop('selected',true)
        $('.district option:eq(0)').prop('selected',true)
    })
;
    //条件查询结束
    //表格部分开始
var pxzd='createTime';
var pxfx ='desc';
var ustates;



    //分页
  function initPagination() {
        // 创建分页
        //获取总行数
        var totalcount= getCount();

        //按照下标进行查询
        $("#Pagination").pagination(totalcount, {
            num_edge_entries: 0, //边缘页数
            num_display_entries: 8, //主体页数
            callback: pageselectCallback, //回调函数
            items_per_page:10, //每页显示10项
            prev_text : "上一页",
            next_text:"下一页",
            ellipse_text:"...",//省略的文本
            prev_show_always:true,//是否总显示“前一页”分页按钮
            next_show_always:true,//是否总显示“下一页”分页按钮，
        });
      $("#currentCount").text("客户数量："+totalcount);
    };


    //表格头部升序 降序

    $('.sequence').on('click',function(){
        pxzd=$(this).attr("pxzd");
       if ( $(this).hasClass('isup')){
           //时间最近
           $(this).removeClass('isup').addClass('isdown');
            pxfx="desc";
           initPagination()

       } else{
           //时间由远到近
           pxfx="asc";
           $(this).removeClass('isdown').addClass('isup');
           initPagination();
       }

    });



    $(".client-status").on("change",
    function () {
          ustates=$(this).val();
        initPagination()
    }
    );
    //全选按钮

    $('#check-all').on('click',function(){
        $('.check-unit').prop('checked',$(this).prop('checked'))
    })
    //批量冻结弹框开始
    $('.freeze').on('click',function(){
       var num =  util.getCheck($('.check-unit')).length;
        if(num == 0 ){
            alert('请先选择要冻结的用户');
            return;
        }
        $('.freeze-quesition').html('确定要将选中的'+ num +'个用户冻结吗?');
        $('.freeze-info').html('冻结后客户将不能实现咨询、关注律师、更换专属律师 、充值等行为。');
        $('#freeze-info').cmsmodal('show');

        //确定
        $('.freeze-confirm').off('click').on('click',function(){
            //ajax
            freeze(util.getCheck($('.check-unit')));
            $('#freeze-info').cmsmodal('hide');
        })
        //取消
        $('.freeze-cancel').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
        })
    })

    //批量冻结弹框结束

    //批量解冻弹框开始
    $('.unfreeze').on('click',function(){
        var num =  util.getCheck($('.check-unit')).length;
        if(num == 0 ){
            alert('请先选择要解冻的用户');
            return;
        }
        $('.freeze-quesition').html('确定要将选中的'+ num +'个用户解冻吗?');
        $('.freeze-info').html('解冻后客户将恢复咨询、关注律师、更换专属律师、充 值等功能。');
        $('#freeze-info').cmsmodal('show');

        //确定
        $('.freeze-confirm').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
            //ajax
            unfreeze();
        })
        //取消
        $('.freeze-cancel').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
        })
    })

    //批量解冻弹框结束
    //编辑弹窗开始





    function edit_btn_show(obj){
       var tr = $(obj).parents("tr");
       var uname= tr.find(".uname").text();//用户名
       var userid=tr.find(".userid").text();//用户id
       var uphone=tr.find(".uphone").text();//手机号
       var ustates=tr.find(".ustates").text();//当前用户状态

        $('#uname_input').val(uname);
        $('#userid_input')["0"].innerHTML=userid;
        $('#uphone_input')["0"].innerHTML=uphone;
        $('#edit-material').cmsmodal('show');

        if(ustates=='正常'){
            $('#ustates_edit').val(1);
        }else{
            $('#ustates_edit').val(2);
        }

    }
    //取消
   function edit_cancel(){
       $('#uname_input').val('');
       $('#userid_input').val('');
       $('#uphone_input').val('');
       $('#ustates_input').val('');
        $('#edit-material').cmsmodal('hide');
    }
    //确定
    function edit_confirm(obj){
        //ajax start
        var toUpJson={
            uname:$('#uname_input')["0"].value,
            userid:$('#userid_input')["0"].innerHTML,
            ustates:$('#ustates_edit').val()
        }

        $.ajax({
            type:"post",
            url:"/user/updateUserInfo",
            data:toUpJson,
            dataType:"json",
            success:function(data){
                //触发查询事件
                initPagination();

            },error:function(  ){
                //触发查询事件
                alert("报错了");
            }

        });
        //ajax end

        $('#edit-material').cmsmodal('hide');

    }



    function  getUphoneArea(){
        var provinceName = $('.province option:selected')["0"].value;
        var provinceId = $('.province option:selected').attr('data-code');//
        var cityId = $('.city option:selected').attr('data-code');
        var districtId = $('.district option:selected').attr('data-code');
        var cityName=$('.city option:selected')["0"].value;
        if(provinceName){
            provinceName=provinceName.replace('省' ,'#');
            provinceName=provinceName.replace('市' ,'#');
        }
        return  provinceName+cityName;
    }



    function  getCount(){
        var result=0;


        //发送ajax;
        //  var selefrom=$("#seek-box").serialize();
        var selefrom={
            uname:$('.client-name')["0"].value,
            uphone:$('.phone-num')["0"].value,
            uphoneArea:getUphoneArea(),
            lawyername:$('.current-lawyer')[0].value,
            gtAtimes:$('.start-num')[0].value,
            lessAtimes:$('.end-num')[0].value,
            createTimestart:$('#create-start')[0].value,
            createTimeend:$('#create-end')[0].value,
            firstTimestart:$('#first-consult-start')[0].value,
            firstTimeend:$('#first-consult-end')[0].value,
            lastTimestart:$('#last-consult-start')[0].value,
            lastTimeend:$('#last-consult-end')[0].value,
            ustates:ustates
        };

        $.ajax(
            {
                type:"post",
                url:"/user/listcount",
                data:selefrom,
                dataType:"json",
                async: false,
                success:function(data){
                    result = data.data;

                },error:function(  ){
                alert("报错了");
            }
            }
        );
        return result;
    }






    function pageselectCallback(page_index, jq){
        query(page_index,jq);
    }




    function getpxzd(){
      var sequences=  $('.sequence');

       return sequences;

    }
    var fields  = new  Array('uname','uphone','uphoneArea','createTime','firstTime','lastTime','lawyername','atimes','ustates','userid');
    function freeze(){
        var arrayObj = new Array();
        var rows = $('.check-unit');
        for(x in rows){
            if(rows[x].checked==true){
                arrayObj.push( $("#table1 tbody tr:eq("+x+") td:nth-child(11)").html());
            }
        }
        var  json={list:arrayObj};
        $.ajax(
            {
                type:"post",
                url:"/user/userfrozeAccount",
                data:json,
                dataType:"json",
                success:function(data){
                    initPagination()
                },error:function(  ){
                alert("报错了");
            }
            }

        );

    }
  function unfreeze(){
        var arrayObj = new Array();
        var rows = $('.check-unit');
        for(x in rows){
            if(rows[x].checked==true){
                arrayObj.push( $("#table1 tbody tr:eq("+x+") td:nth-child(11)").html());
            }
        }
        var  json={list:arrayObj};
        $.ajax(
            {
                type:"post",
                url:"/user/userunfrozeAccount",
                data:json,
                dataType:"json",
                success:function(data){
                    initPagination()
                },error:function(  ){
                alert("报错了");
            }
            }

        );

    }

//   表格部分结束





function query( page_index, jq){

    var provinceId = $('.province option:selected').attr('value');//
    var cityId = $('.city option:selected').attr('data-code');
    var provinceName = $('.province option:selected')["0"].value;
    var districtId = $('.district option:selected').attr('data-code');
    var cityName=$('.city option:selected')["0"].value;
    //姓名 、手机号、手机号归属地、账号创建时间、首次咨询时间、末次咨询时间、当前专属律师、咨询总次数、状态，操作
    var firsttime=false;
        if(typeof(page_index)=="undefined"){
          page_index=0;
          firsttime=true;
        }
    if(provinceName){
        provinceName=provinceName.replace('省' ,'#');
        provinceName=provinceName.replace('市' ,'#');
    }
    //发送ajax;
    //  var selefrom=$("#seek-box").serialize();
    var selefrom={
        uname:$('.client-name')["0"].value,
        uphone:$('.phone-num')["0"].value,
        uphoneArea:getUphoneArea(),
        lawyername:$('.current-lawyer')[0].value,
        gtAtimes:$('.start-num')[0].value,
        lessAtimes:$('.end-num')[0].value,
        createTimestart:$('#create-start')[0].value,
        createTimeend:$('#create-end')[0].value,
        firstTimestart:$('#first-consult-start')[0].value,
        firstTimeend:$('#first-consult-end')[0].value,
        lastTimestart:$('#last-consult-start')[0].value,
        lastTimeend:$('#last-consult-end')[0].value,
        pageNum:page_index+1,
        pxzd:pxzd,
        pxfx:pxfx,
        ustates:ustates
    };

    $.ajax(
        {
            type:"post",
            url:"/user/userlist",
            data:selefrom,
            dataType:"json",
            success:function(data){
                var result = data.data;
                var tmpl1 = bt('list1', result);
                $("#tbody1").html(tmpl1);
              // initPagination;
            },error:function(  ){
            alert("报错了");
        }
        }
    );

    //按照下标进行查询

}